﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using MathematicallySafe.HeadCounter.Common;
using MathematicallySafe.HeadCounter.ViewModels;

namespace MathematicallySafe.HeadCounter.Views
{
    /// <summary>
    /// Interaction logic for RaidImportView.xaml
    /// </summary>
    public partial class RaidImportView : UserControl
    {
        public RaidImportView()
        {
            InitializeComponent();
        }

        public RaidImportViewModel ViewModel
        {
            get { return DataContext as RaidImportViewModel; }
            set { DataContext = value; }
        }

        private void RaidImportViewInitialized(object sender, EventArgs e)
        {
            ViewModel = new RaidImportViewModel();
            ViewModel.Initialise();
        }

        // todo Possibly the tab control can reflect its view model much like IsSelected in wpf tree controls so we can do this via binding not code behind.
        private void RaidImportViewIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ViewModel.IsViewInScope = (bool) e.NewValue;
        }

        private void HyperlinkOnRequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            Utilities.HyperlinkRequestNavigate(sender, e);
        }

        // todo delegate commands for drag and drop perhaps?
        private void RaidImportViewDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.UnicodeText))
            {
                DoTextDrop(e);
            }

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                DoFileDrop(e);
            }

            // todo activate the application on drop.
            // find parent window via visual tree i guess.
        }

        private void DoTextDrop(DragEventArgs e)
        {
            object dropData = e.Data.GetData(DataFormats.UnicodeText);
            ViewModel.BeginRaidImport(dropData as string);
        }

        private void DoFileDrop(DragEventArgs e)
        {
            try
            {
                var droppedFiles = (Array) e.Data.GetData(DataFormats.FileDrop);

                if (droppedFiles != null && droppedFiles.Length > 0)
                {
                    string droppedFile = droppedFiles.GetValue(0).ToString();

                    // Queue file drop on the dispatcher so we dont hang up the source
                    Dispatcher.BeginInvoke(new Action(() => LoadDroppedFile(ViewModel, droppedFile)));
                }
            }
            catch (Exception)
            {
                // Exceptions are unlikely but swallow them just in case.
            }
        }

        private readonly Action<RaidImportViewModel, string> LoadDroppedFile =
           (viewModel, filename) => viewModel.BeginRaidImportFromFile(filename);
    }
}